package com.gz.EuchreU.Controls;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.input.touch.TouchEvent;

import com.gz.EuchreU.Contants.CardValues;
import com.gz.EuchreU.Contants.Suits;
import com.gz.EuchreU.Sprite.Card;

public abstract class RatingCards extends Entity{

	private final float buffer = 3;
	
	private Card mDiamondCard;
	private Card mClubCard;
	private Card mHeartCard;
	private Card mSpadeCard;
	
	private int mValue = 0;
	
	public RatingCards(final float pX, final float pY, final float pCardWidth, Scene pScene){
		float x = pX;
		float w = pCardWidth;
		mDiamondCard = new Card(x,pY,w,Suits.DIAMONDS, CardValues.ACE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					setValue(1);
					UpdateValues();
				}
				return true;
			}
		};
		x+= mDiamondCard.getWidth() + buffer;
		mClubCard = new Card(x,pY,w,Suits.CLUBS, CardValues.ACE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					setValue(2);
					UpdateValues();
				}
				return true;
			}
		};
		
		x+= mDiamondCard.getWidth() + buffer;
		mHeartCard = new Card(x,pY,w,Suits.HEARTS, CardValues.ACE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					setValue(3);
					UpdateValues();
				}
				return true;
			}
		};
		x+= mDiamondCard.getWidth() + buffer;
		mSpadeCard = new Card(x,pY,w,Suits.SPADES, CardValues.ACE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					setValue(4);
					UpdateValues();
				}
				return true;
			}
		};
		
		attachChild(mDiamondCard);
		attachChild(mClubCard);
		attachChild(mHeartCard);
		attachChild(mSpadeCard);

		pScene.registerTouchArea(mDiamondCard);
		pScene.registerTouchArea(mClubCard);
		pScene.registerTouchArea(mHeartCard);
		pScene.registerTouchArea(mSpadeCard);
		UpdateValues();
	}
	
	public abstract void OnValueChanged(int pNewValue);
	
	public int getValue(){
		return mValue;
	}
	
	private void setValue(int pValue){
		setValue(pValue,true);
	}
	
	public void setValue(int pValue, boolean pEvent){
		if(pValue <= 0)mValue = 0;
		else if(pValue >= 4)mValue = 4;
		else mValue = pValue;
		
		if(pEvent){
			OnValueChanged(mValue);
		}
		
		UpdateValues();
	}
	
	private void UpdateValues(){
		switch(mValue){
		case 0:
			mDiamondCard.setDisabled(true);
			mClubCard.setDisabled(true);
			mHeartCard.setDisabled(true);
			mSpadeCard.setDisabled(true);
			break;
		case 1:
			mDiamondCard.setDisabled(false);
			mClubCard.setDisabled(true);
			mHeartCard.setDisabled(true);
			mSpadeCard.setDisabled(true);
			break;
		case 2:
			mDiamondCard.setDisabled(false);
			mClubCard.setDisabled(false);
			mHeartCard.setDisabled(true);
			mSpadeCard.setDisabled(true);
			break;
		case 3:
			mDiamondCard.setDisabled(false);
			mClubCard.setDisabled(false);
			mHeartCard.setDisabled(false);
			mSpadeCard.setDisabled(true);
			break;
		default:
			mDiamondCard.setDisabled(false);
			mClubCard.setDisabled(false);
			mHeartCard.setDisabled(false);
			mSpadeCard.setDisabled(false);
			break;
		}
	}
	
}
